#include <iostream>
using namespace std;

// 普通写法
class Calculator
{
public:
  int m_num1;
  int m_num2;

  int getResult(string oper)
  {
    if (oper == "+")
    {
      return m_num1 + m_num2;
    }
    else if (oper == "-")
    {
      return m_num1 - m_num2;
    }
    else if (oper == "*")
    {
      return m_num1 * m_num2;
    }
    // 如果想扩展新的功能，需要修改源码
    // 在真实开发中，提倡 开闭原则
    // 开闭原则：对扩展进行开放，对修改进行关闭
    return 0;
  }
};

void test01()
{
  Calculator c;
  c.m_num1 = 10;
  c.m_num2 = 20;

  cout << "num1 + num2 = " << c.getResult("+") << endl;
  cout << "num1 - num2 = " << c.getResult("-") << endl;
  cout << "num1 * num2 = " << c.getResult("*") << endl;
}

// 多态写法
// 多态好处
// 1. 组织结构清晰
// 2. 可读性强
// 3. 对于前期和后期的扩展以及可维护性高

// 1. 实现计算器抽象类
class AbstractCalculator
{
public:
  int m_num1;
  int m_num2;

  virtual int getResult()
  {
    return 0;
  }
};

// 加法计算器类
class AddCalculator : public AbstractCalculator
{
public:
  virtual int getResult()
  {
    return m_num1 + m_num2;
  }
};

// 减法计算器类
class SubCalculator : public AbstractCalculator
{
public:
  virtual int getResult()
  {
    return m_num1 - m_num2;
  }
};

// 乘法计算器类
class MulCalculator : public AbstractCalculator
{
public:
  virtual int getResult()
  {
    return m_num1 * m_num2;
  }
};

void test02()
{
  // 多态使用条件
  // 父类指针或者引用指向子类对象

  // 加法
  AbstractCalculator *abc = new AddCalculator;
  abc->m_num1 = 100;
  abc->m_num2 = 200;

  cout << abc->m_num1 << " + " << abc->m_num2 << " = " << abc->getResult() << endl;
  // 使用完后销毁
  delete abc;

  // 减法
  abc = new SubCalculator;

  abc->m_num1 = 100;
  abc->m_num2 = 200;

  cout << abc->m_num1 << " - " << abc->m_num2 << " = " << abc->getResult() << endl;
  delete abc;

  // 乘法
  abc = new MulCalculator;

  abc->m_num1 = 100;
  abc->m_num2 = 200;

  cout << abc->m_num1 << " * " << abc->m_num2 << " = " << abc->getResult() << endl;
  delete abc;
}

int main(int argc, char const *argv[])
{
  test01();
  test02();

  return 0;
}